package cn.iocoder.yudao.module.member.service.memberconf.confmaterial;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.confmaterial.vo.MemberConfMaterialPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.confmaterial.vo.MemberConfMaterialSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confmaterial.MemberConfMaterialDO;
import cn.iocoder.yudao.module.member.dal.mysql.memberconf.confmaterial.MemberConfMaterialMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.CONF_MATERIAL_NOT_EXISTS;

/**
 * 物料策略配置 Service 实现类
 *
 * @author doock
 */
@Service
@Validated
public class MemberConfMaterialServiceImpl implements MemberConfMaterialService {

    @Resource
    private MemberConfMaterialMapper confMaterialMapper;

    @Override
    public Long createMemberConfMaterial(MemberConfMaterialSaveReqVO createReqVO) {
        // 插入
        MemberConfMaterialDO confMaterial = BeanUtils.toBean(createReqVO, MemberConfMaterialDO.class);
        confMaterialMapper.insert(confMaterial);
        // 返回
        return confMaterial.getId();
    }

    @Override
    public void updateMemberConfMaterial(MemberConfMaterialSaveReqVO updateReqVO) {
        // 校验存在
        validateMemberConfMaterialExists(updateReqVO.getId());
        // 更新
        MemberConfMaterialDO updateObj = BeanUtils.toBean(updateReqVO, MemberConfMaterialDO.class);
        confMaterialMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberConfMaterial(Long id) {
        // 校验存在
        validateMemberConfMaterialExists(id);
        // 删除
        confMaterialMapper.deleteById(id);
    }

    private void validateMemberConfMaterialExists(Long id) {
        if (confMaterialMapper.selectById(id) == null) {
            throw exception(CONF_MATERIAL_NOT_EXISTS);
        }
    }

    @Override
    public MemberConfMaterialDO getMemberConfMaterial(Long id) {
        return confMaterialMapper.selectById(id);
    }


    @Override
    public PageResult<MemberConfMaterialDO> getMemberConfMaterialPage(MemberConfMaterialPageReqVO pageReqVO) {
        return confMaterialMapper.selectPage(pageReqVO);
    }

}